package com.magicsoft.share.ui.article;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.base.BasePresenter;
import com.magicsoft.share.utils.KeyboardUtils;
import com.magicsoft.share.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class EdtTitleActivity extends BaseActivity {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.edt)
    EditText edt;
    @BindView(R.id.tv_num)
    TextView tvNum;
    private String titleStr;

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_edt_title;
    }

    @Override
    public void initData() {

        titleStr = getIntent().getStringExtra("titleStr");
    }

    @Override
    public void initView() {
        ibtnLeft.setVisibility(View.GONE);
        tvLeft.setText("取消");
        tvRight.setText("完成");
        tvTitle.setText("编辑标题");
        edt.setText(titleStr);

        edt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int i = 50 - editable.length();
                tvNum.setText("您还可以输入" + i + "字");
            }
        });

    }

    @Override
    public void requestData() {

    }

    @OnClick({R.id.tv_left, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                //此处修改
                KeyboardUtils.closeKeybord(edt,this);
                if (edt.getText().length() == 0||edt.getText().toString().trim().equals("")) {
                    ToastUtils.showToast(this, "请输入标题");
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("titleStr", edt.getText().toString());
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;
            case R.id.tv_right:
                KeyboardUtils.closeKeybord(edt,this);
                String titleStr = edt.getText().toString();
                if (edt.getText().length() == 0||edt.getText().toString().trim().equals("")) {
                    ToastUtils.showToast(this, "请输入标题");
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("titleStr", titleStr);
                    setResult(RESULT_OK, intent);
                    finish();
                }

                break;
        }
    }
}
